//
//  BaseTabBarController.m
//  Theme
//
//  Created by 张鹏伟 on 15/8/21.
//  Copyright (c) 2015年 108. All rights reserved.
//

#import "BaseTabBarController.h"
#import "BaseNavigationController.h"

@interface BaseTabBarController ()

@end

@implementation BaseTabBarController


//创建TabBar单例,防止多次创建
+(BaseTabBarController *) sharedInstance{
    
    static BaseTabBarController *sharedInstace = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstace = [[self alloc] init];
    });
    
    return sharedInstace;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置tabBar的背景颜色
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)];
    _bgView.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"bgColor"];
    [self.tabBar insertSubview:_bgView atIndex:0];
    self.tabBar.opaque = YES;
    
    //快讯控制器
    _fastNewsVC = [[FastNewsController alloc] init];
    BaseNavigationController *fastNewsNavigation = [[BaseNavigationController alloc] initWithRootViewController:_fastNewsVC];
    
    [self setTabBarItem:_fastNewsVC.tabBarItem Title:@"快讯" withTitleSize:13.0 selectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"selectIcon-01_00.png"] withTitleColor:RGB(21, 95, 216, 1.0f)unselectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"icon-01_00.png"] withTitleColor:RGB(108, 108, 108, 1.0f)];
    
    _fastNewsVC.tabBarItem.badgeValue = @"新";
    
    //要闻控制器
    _informationVC = [[InformationController alloc] init];
    BaseNavigationController *informationNavigation = [[BaseNavigationController alloc] initWithRootViewController:_informationVC];
    
    [self setTabBarItem:_informationVC.tabBarItem Title:@"要闻" withTitleSize:13.0 selectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"selectIcon-01_01.png"] withTitleColor:RGB(21, 95, 216, 1.0f)unselectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"icon-01_01.png"] withTitleColor:RGB(108, 108, 108, 1.0f)];
    
    //行情控制器
    _priceVC = [[PriceController alloc] init];
    BaseNavigationController *priceNavigation = [[BaseNavigationController alloc] initWithRootViewController:_priceVC];
    
    [self setTabBarItem:_priceVC.tabBarItem Title:@"行情" withTitleSize:13.0 selectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"selectIcon-01_02.png"] withTitleColor:RGB(21, 95, 216, 1.0f)unselectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"icon-01_02.png"] withTitleColor:RGB(108, 108, 108, 1.0f)];
    
    //日历控制器
    _dataVC = [[DataController alloc] init];
    BaseNavigationController *dataNavigation = [[BaseNavigationController alloc] initWithRootViewController:_dataVC];
    
    [self setTabBarItem:_dataVC.tabBarItem Title:@"日历" withTitleSize:13.0 selectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"selectIcon-01_03.png"] withTitleColor:RGB(21, 95, 216, 1.0f)unselectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"icon-01_03.png"] withTitleColor:RGB(108, 108, 108, 1.0f)];
    
    //我的控制器
    _settingVC = [[SettingController alloc] init];
    BaseNavigationController *settingNavigation = [[BaseNavigationController alloc] initWithRootViewController:_settingVC];
    
    [self setTabBarItem:_settingVC.tabBarItem Title:@"我" withTitleSize:13.0 selectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"selectIcon-01_04.png"] withTitleColor:RGB(21, 95, 216, 1.0f)unselectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"icon-01_04.png"] withTitleColor:RGB(108, 108, 108, 1.0f)];
    
    self.viewControllers = @[fastNewsNavigation,informationNavigation,priceNavigation,dataNavigation,settingNavigation];
    
    //接收主题改变的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeThemeNotificantion) name:ThemeChangeNotification object:nil];
    
    [self changeThemeNotificantion];

    // Do any additional setup after loading the view.
}

//改变主题的通知
- (void)changeThemeNotificantion{
    
    _bgView.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"bgColor"];

    [self setTabBarItem:_fastNewsVC.tabBarItem Title:@"快讯" withTitleSize:13.0 selectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"selectIcon-01_00.png"] withTitleColor:RGB(21, 95, 216, 1.0f)unselectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"icon-01_00.png"] withTitleColor:RGB(108, 108, 108, 1.0f)];
    
    [self setTabBarItem:_informationVC.tabBarItem Title:@"要闻" withTitleSize:13.0 selectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"selectIcon-01_01.png"] withTitleColor:RGB(21, 95, 216, 1.0f)unselectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"icon-01_01.png"] withTitleColor:RGB(108, 108, 108, 1.0f)];
    
    [self setTabBarItem:_priceVC.tabBarItem Title:@"行情" withTitleSize:13.0 selectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"selectIcon-01_02.png"] withTitleColor:RGB(21, 95, 216, 1.0f)unselectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"icon-01_02.png"] withTitleColor:RGB(108, 108, 108, 1.0f)];
    
    [self setTabBarItem:_dataVC.tabBarItem Title:@"日历" withTitleSize:13.0 selectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"selectIcon-01_03.png"] withTitleColor:RGB(21, 95, 216, 1.0f)unselectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"icon-01_03.png"] withTitleColor:RGB(108, 108, 108, 1.0f)];
    
    [self setTabBarItem:_settingVC.tabBarItem Title:@"我" withTitleSize:13.0 selectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"selectIcon-01_04.png"] withTitleColor:RGB(21, 95, 216, 1.0f)unselectedImage:[[ConfigurationTheme shareInstance] getThemeImageName:@"icon-01_04.png"] withTitleColor:RGB(108, 108, 108, 1.0f)];
    
}

- (void)setTabBarItem:(UITabBarItem *)tabbarItem
                Title:(NSString *)title
        withTitleSize:(CGFloat)size
        selectedImage:(UIImage *)selectedImage
       withTitleColor:(UIColor *)selectColor
      unselectedImage:(UIImage *)unselectedImage
       withTitleColor:(UIColor *)unselectColor{
    
    //设置图片
    tabbarItem = [tabbarItem initWithTitle:title image:[unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    //未选中字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:unselectColor,NSFontAttributeName:[UIFont systemFontOfSize:size]} forState:UIControlStateNormal];
    
    //选中字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:selectColor,NSFontAttributeName:[UIFont systemFontOfSize:size]} forState:UIControlStateSelected];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
